package com.whmskj.xjlhsc.qianYiData.controller;

import com.whmskj.xjlhsc.qianYiData.service.The21stService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/qianYiData/the21st")
@RequiredArgsConstructor
public class The21stController {

    private final The21stService the21stService;

    /**
     * 手动同步（按基准日计算“21~20”窗口）
     * 用法：
     * - POST /qianYiData/the21st/sync              -> 基于今天
     * - POST /qianYiData/the21st/sync?base=2025-09-06  -> 基于指定日期
     */
    @PostMapping("/sync")
    public Map<String, Object> sync(@RequestParam(required = false) String base) {
        LocalDate baseDate = (base == null || base.isEmpty()) ? LocalDate.now() : LocalDate.parse(base);
        Window w = calcWindow(baseDate);
        int affected = the21stService.syncByBaseDate(baseDate);
        Map<String, Object> resp = new HashMap<>();
        resp.put("base", baseDate.toString());
        resp.put("month1", w.monthFirst.toString());
        resp.put("startDay", w.startDay.toString());
        resp.put("endDay", w.endDay.toString());
        resp.put("affected", affected);
        return resp;
    }

    /**
     * 手动同步指定归属月份（month1），窗口为上月21~该月20
     * 用法：POST /qianYiData/the21st/syncMonth?month=2025-09
     */
    @PostMapping("/syncMonth")
    public Map<String, Object> syncMonth(@RequestParam String month) {
        YearMonth ym = YearMonth.parse(month);
        LocalDate baseDate = ym.atDay(1); // 1号(<=20) -> 窗口=上月21~本月20，month1=本月1号
        Window w = calcWindow(baseDate);
        int affected = the21stService.syncByBaseDate(baseDate);
        Map<String, Object> resp = new HashMap<>();
        resp.put("month", ym.toString());
        resp.put("base", baseDate.toString());
        resp.put("month1", w.monthFirst.toString());
        resp.put("startDay", w.startDay.toString());
        resp.put("endDay", w.endDay.toString());
        resp.put("affected", affected);
        return resp;
    }

    private Window calcWindow(LocalDate baseDate) {
        int day = baseDate.getDayOfMonth();
        if (day <= 20) {
            LocalDate monthFirst = baseDate.withDayOfMonth(1);
            LocalDate start = baseDate.minusMonths(1).withDayOfMonth(21);
            LocalDate end = baseDate.withDayOfMonth(20);
            return new Window(monthFirst, start, end);
        } else {
            YearMonth next = YearMonth.from(baseDate).plusMonths(1);
            LocalDate monthFirst = next.atDay(1);
            LocalDate start = baseDate.withDayOfMonth(21);
            LocalDate end = next.atDay(20);
            return new Window(monthFirst, start, end);
        }
    }

    private static class Window {
        final LocalDate monthFirst;
        final LocalDate startDay;
        final LocalDate endDay;
        Window(LocalDate monthFirst, LocalDate startDay, LocalDate endDay) {
            this.monthFirst = monthFirst;
            this.startDay = startDay;
            this.endDay = endDay;
        }
    }
}

